package com.renliwo.processor.service;

import com.alibaba.fastjson.JSON;
import com.renliwo.processor.common.FastJsonUtil;
import com.renliwo.processor.json.AuditContent;
import com.renliwo.processor.json.InsuranceInfo;
import com.renliwo.processor.json.ProductInfo;
import com.renliwo.processor.json.RmDisabilityGoldInfo;
import com.renliwo.processor.mapper.auto.TAuditLogPoMapper;
import com.renliwo.processor.mapper.auto.TAuditPoMapper;
import com.renliwo.processor.model.TAuditLogPo;
import com.renliwo.processor.model.TAuditPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Louis on 2017/9/21.
 */
@Service
public class PhaseIICityModifyService {

    @Autowired
    private TAuditPoMapper auditPoMapper;

    @Autowired
    private TAuditLogPoMapper auditLogPoMapper;

    @Transactional
    public void forceConscienceAudit(){
       List<TAuditPo> auditPoList = auditPoMapper.selectAll();

       if (auditPoList != null && !auditPoList.isEmpty()){
           for (TAuditPo po : auditPoList){
               ProductInfo contentBeforeModify = FastJsonUtil.toBean(po.getContentBeforeModify(), ProductInfo.class);
               ProductInfo contentAfterModify = FastJsonUtil.toBean(po.getContentAfterModify(), ProductInfo.class);

               updateContent(contentBeforeModify);

               updateContent(contentAfterModify);


               TAuditPo updatePo = new TAuditPo();
               updatePo.setId(po.getId());
               updatePo.setContentBeforeModify(JSON.toJSONString(contentBeforeModify));
               updatePo.setContentAfterModify(JSON.toJSONString(contentAfterModify));

               auditPoMapper.updateByPrimaryKeySelective(updatePo);

           }
       }

    }

    @Transactional
    public void forceConsienceAuditLog(){
        List<TAuditLogPo> tAuditLogPoList = auditLogPoMapper.selectAll();

        if (tAuditLogPoList != null && !tAuditLogPoList.isEmpty()){
            for (TAuditLogPo po : tAuditLogPoList){
                if (po.getAuditContent() == null){
                    continue;
                }

                AuditContent auditContent = FastJsonUtil.toBean(po.getAuditContent(), AuditContent.class);

                if (auditContent.getContentBeforeModify() !=null){
                    ProductInfo contentBeforeModify = FastJsonUtil.toBean(auditContent.getContentBeforeModify(), ProductInfo.class);
                    updateContent(contentBeforeModify);
                    auditContent.setContentBeforeModify(JSON.toJSONString(contentBeforeModify));
                }


                if (auditContent.getContentAfterModify() != null){
                    ProductInfo contentAfterModify = FastJsonUtil.toBean(auditContent.getContentAfterModify(), ProductInfo.class);
                    updateContent(contentAfterModify);
                    auditContent.setContentAfterModify(JSON.toJSONString(contentAfterModify));
                }

                TAuditLogPo updatePo = new TAuditLogPo();
                updatePo.setId(po.getId());
                updatePo.setAuditContent(JSON.toJSONString(auditContent));

                auditLogPoMapper.updateByPrimaryKeySelective(updatePo);

            }
        }
    }

    private void updateContent(ProductInfo content) {
        if (content != null && content.getIsForceConscience() != null){
            if (content.getInsuranceFormList() != null && !content.getInsuranceFormList().isEmpty()){
                for (InsuranceInfo insuranceInfo : content.getInsuranceFormList()){
                    insuranceInfo.setIsForceConscience(content.getIsForceConscience());
                }
            }

            if (content.getRmDisabilityGoldForms() != null && !content.getRmDisabilityGoldForms().isEmpty()){
                for (RmDisabilityGoldInfo rmDisabilityGoldInfo : content.getRmDisabilityGoldForms()){
                    rmDisabilityGoldInfo.setIsForceConscience(content.getIsForceConscience());
                }
            }
        }
    }
}
